package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysUser;
import com.markerhub.entity.TFacilityBase;
import com.markerhub.entity.TProjectBase;
import com.markerhub.service.TFacilityBaseService;
import com.markerhub.service.TProjectBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘佳明
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/tProjectBase")
public class TProjectBaseController {
    @Autowired
    TProjectBaseService tProjectBaseService;
    @Autowired
    TFacilityBaseService tFacilityBaseService;


    @PostMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:project:list')")
    public Result INFO(@PathVariable("id") Integer id){
        TProjectBase byId = tProjectBaseService.getById(id);
        return  Result.succ(byId);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:project:list')")
    public Result list(Integer size, Integer current,String projectname,String projectphone) {

        if (size==0&&current==0){
            List<TProjectBase> list = tProjectBaseService.list();
            return Result.succ(list);
        }
        //current=(current-1)*size;
        IPage page1 = new Page(current, size);
        QueryWrapper<TProjectBase> queryWrapper = new QueryWrapper<TProjectBase>();
        if (projectname!= "") {
            queryWrapper.like(StrUtil.isNotBlank(projectname), "project_name", projectname);
        }
        else if(projectphone!="") {
            queryWrapper.like(StrUtil.isNotBlank(projectphone), "phone1", projectphone).or()
                    .like(StrUtil.isNotBlank(projectphone), "phone2", projectphone);
        }

        IPage<TProjectBase> pageData = tProjectBaseService.page(page1, queryWrapper);
        //tProjectBaseService.getProjectList()

        return Result.succ(pageData);
    }
    // 新增项目信息
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:project:add')")
    public  Result ADD(@Validated @RequestBody TProjectBase tProjectBase){
        System.out.println(tProjectBase);
        tProjectBase.setCreated(LocalDateTime.now());
        tProjectBaseService.save(tProjectBase);
        return  Result.succ(tProjectBase);

    }
    /*
     * 修改项目 信息
     * */
    @PostMapping("/updata")
    @PreAuthorize("hasAuthority('sys:project:updata')")
    public  Result updata(@Validated @RequestBody TProjectBase tProjectBase){
        System.out.println(tProjectBase);
        tProjectBase.setCreated(LocalDateTime.now());
        tProjectBaseService.updateById(tProjectBase);
        return  Result.succ(tProjectBase);

    }

    //    s删除一个项目信息
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys:project:delete')")
    public  Result delete(String projectid){
        List<TFacilityBase> projectid1 = tFacilityBaseService.list(new QueryWrapper<TFacilityBase>().eq("projectid", projectid));
        if (projectid1.size()>0){
            return Result.fail("该项目下还有设备信息，不能删除！！");
        }
        boolean b = tProjectBaseService.removeById(projectid);
        return Result.succ(b);
    }
}

